package sctu.edu.java.year2016.team02.project.service.UI;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

/**
 * Created by Eoly on 2016/6/16.
 */
//服务端的主要界面，用来显示与客服端的交流与各种故障
public class MainUI extends JFrame implements ActionListener{
    public static int screenWidth= Toolkit.getDefaultToolkit().getScreenSize().width;
    public static int ScreenHeight=Toolkit.getDefaultToolkit().getScreenSize().height;

    private JRadioButton startService;
    private JTextArea  state;
    private JLabel tip;
    private StartAndClose connThr = null;
    public MainUI(){
        setTitle("服务端");
        setBounds(screenWidth / 4, ScreenHeight / 4, 400, 300);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        Init();
        setLayout(null);
        setResizable(false);
        setVisible(true);
        ipSet.setIp();
    }
    private void Init(){
        startService = new JRadioButton("开启服务", false);
        state = new JTextArea();
        state.setLineWrap(true);
        state.setEditable(false);
        addScorll(state, 20, 40, 360, 200);
        startService.setBounds(20, 10, 100, 25);
        add(startService);
        startService.addActionListener(this);
    }

    private void addScorll(JTextArea jta,int x,int y,int weight,int high){
        JScrollPane newJScroll = new JScrollPane(jta);
        newJScroll.setBounds(x,y,weight,high);
        add(newJScroll);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        if(e.getSource()==startService){
            boolean stSeIsSelected=startService.isSelected();
            System.out.println("isSele:"+stSeIsSelected);
            if(stSeIsSelected) {
                connThr = new StartAndClose(tip, state,startService, true);
                new Thread(connThr).start();
            }else{
                connThr.isStart=false;
                startService.setEnabled(false);
                connThr.setStateText("关闭服务中...","/local");
            }
        }
    }
}
